import 'package:flutter/material.dart';
import 'mock_data.dart';

class MinePage extends StatelessWidget {
  static const MARGIN = 8.0;
  static const INNER_MARGIN = 16.0;

  const MinePage({Key? key}) : super(key: key);


  Column _getMenus(String? icon,String? name,{Color color = Colors.black}){
    return Column(
      mainAxisAlignment:MainAxisAlignment.center,
      children: <Widget>[
        SizedBox(
          child:Image.asset(icon!),
          width: 50,
          height: 50,
        ),
        SizedBox(
          height:5,
        ),
        Text(name!,style:TextStyle(fontSize:14.0,color:color,height:2)),
      ],
    );
  }

  Widget _headerGridButton(){
    double height = 144;
    List<Map<String,String>> buttons = GridMockData.headerGrids();
    return Container(
      height:height,
      margin:EdgeInsets.fromLTRB(MARGIN,MARGIN,MARGIN,MARGIN/2),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4.0),
        gradient: LinearGradient(
          begin:Alignment.topCenter,
          end:Alignment.bottomCenter,
          colors:[
            Color(0xFF56AF6D),
            Color(0XFF56AA6D),
          ],
        ),
      ),
      child:Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: buttons
                      .map((item) =>
                                  _getMenus(item['icon'],item['name'],color:Colors.white)).toList(),
        ),
      ),
    );
  }


  GridView _gridButtons(List<Map<String,String>> buttons,int crossAxisCount,{Color textColor = Colors.white}){
    double gridSpace = 5.0;
    return GridView.count(
      crossAxisSpacing: gridSpace,
      mainAxisSpacing: gridSpace,
      crossAxisCount: crossAxisCount,
      shrinkWrap:true,
      physics:NeverScrollableScrollPhysics(),
      children: buttons.map(
          (item) {
            return _getMenus(item['icon'],item['name'],color:textColor);
          }
      ).toList(),
    );
  }

  Widget _dynamicGridButtons(List<Map<String,String>> buttons,String title, {int crossAxisCount = 4}){
    return Container(
        margin:EdgeInsets.fromLTRB(MARGIN, MARGIN, MARGIN, MARGIN /2 ),
        padding:EdgeInsets.all(MARGIN),
        decoration:BoxDecoration(
          borderRadius: BorderRadius.circular(4.0),
          color: Colors.white,
        ),
      child: Column(
        mainAxisAlignment:MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title,style: TextStyle(color:Colors.grey[700])),
          SizedBox(height:20),
          _gridButtons(buttons,crossAxisCount,textColor:Colors.black),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          _headerGridButton(),
          _dynamicGridButtons(GridMockData.financeGrids(),'金融理财'),
          _dynamicGridButtons(GridMockData.serviceGrids(),'生活服务'),
          _dynamicGridButtons(GridMockData.thridpartyGrids(),'购物消费'),
        ],
      )
    );
  }
}


